const n9 = {
    ref(vm, name) {
        return vm.$refs[name];
    },
    success(message) {
        $Message.success({
            content: message,
            duration: 2,
            closable: true
        });
    },
    error(message) {
        $Message.error({
            content: message,
            duration: 4,
            closable: true
        });
    },
    input(title, defaultValue, okCallback, okName) {
        var ret = '';
        $Modal.confirm({
            okText: okName,
            title: title,
            loading: true,
            render: (h) => {
                return h('Input', {
                    props: {
                        value: defaultValue,
                        autofocus: true,
                        placeholder: '请输入'
                    },
                    style: {
                        'margin-top': '10px'
                    },
                    on: {
                        input: (val) => {
                            ret = val.trim();
                        }
                    }
                });
            },
            onOk: function () {
                if (ret == '') {
                    n9.error('请输入内容');
                    this.buttonLoading = false;
                    return;
                }
                if (okCallback) {
                    if (okCallback(ret) == false) {
                        this.buttonLoading = false;
                        return;
                    }
                }
                $Modal.remove();
            }
        });
    },
    confirm(title, content, okCallback, okName) {
        $Modal.confirm({
            title: title,
            content: "<span style='font-size:14px'>" + content + "<span>",
            loading: true,
            okText: okName,
            onOk: () => {
                if (okCallback) {
                    okCallback();
                }
                $Modal.remove();
            }
        })
    },
    /**
     * 打开弹窗
     * @param page 页面
     * @param title 标题
     * @param icon 图标
     * @param params 传递的参数json
     * @param callback 回调
     * @param options {css: page的样式, cancelName: 取消按钮文本, okName: 确定按钮文本, multiSelect: 是否多选, okBtn: 是否显示ok按钮}
     */
    openDialog(page, title, icon, params, callback, options) {
        const instance = new N9Dialog({
            data() {
                var ret = {
                    title: title,
                    page: page,
                    icon: icon,
                    params: params,
                    callback: callback
                };
                if (options) {
                    n9.copyJsonAll(options, ret);
                }
                return ret;
            }
        });
    },
    /**
     * 打开右键菜单
     * @param x 菜单水平位置
     * @param y 菜单垂直位置
     * @param model 数据列表，如果是字符串数组，则直接显示字符串，如果是json数组，则显示item.name属性值
     * @param actionListener 回调事件 返回当前选中的item, index
     * @param icon 图标，true,false,{icon}
     * @returns {{top: *, left: *, model: *, actionListener: *, icon: boolean}}
     */
    openContextMenu(x, y, model, actionListener, icon) {
        const instance = new N9ContextMenu({
            data() {
                return {
                    top: y,
                    left: x,
                    model: model,
                    actionListener: actionListener,
                    icon: icon == undefined ? false : icon
                };
            }
        });
    },
    validate: {
        required(msg) {
            return { required: true, message: msg || '必填项不能为空', trigger: 'change' };
        },
        requiredNumber(msg) {
            return { required: true, type: 'number', message: msg || '必填项不能为空', trigger: 'change' };
        },
        requiredArray(msg) {
            return { required: true, type: 'array', message: msg || '必填项不能为空', trigger: 'change' };
        },
        length(min, max, msg) {
            if (!max || max <= 0) {
                return {min: min, message: msg || '长度不能小于' + min, trigger: 'change' };
            } else if (!min || min <= 0) {
                return {max: max, message: msg || '长度不能大于' + max, trigger: 'change' };
            }
            return {min: min, max: max, message: msg || '长度不能小于' + min + ", 且不能大于" + max, trigger: 'change' };
        },
        date(msg) {
            return { type: 'string', pattern: /^(20)(\d{2})-(\d{2})-(\d{2})$/, message: msg || '日期格式不正确', trigger: 'change' };
        },
        time(msg) {
            return { type: 'string', pattern: /^(20|21|22|23|[0-1]?\d):[0-5]\d$/, message: msg || '时间格式不正确', trigger: 'change' };
        },
        money(msg) {
            return { type: 'string', pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: msg || '金额格式不正确', trigger: 'change' };
        },
        tel(msg) {
            return { type: 'string', pattern: /^1\d{10}$/, message: msg || '手机号码格式不正确', trigger: 'change' };
        },
        custom(check) {
            return {validator: (rule, value, callback) => {
                    var ret = check(value);
                    if (!ret) {
                        callback();
                    }
                    callback(new Error(ret));
                }, trigger: 'change'};
        }
    },
    getTitleFromButton(event) {
        return event.currentTarget.innerText.trim();
    },
    getIconFromButton(event) {
        return event.currentTarget.children[0].classList[1].ltrim('ivu-icon-');
    },
    copyJson(from, to, ...keys) {
        keys.forEach((key)=> {
            if (from[key] !== undefined) {
                to[key] = from[key];
            }
        });
    },
    copyJsonAll(from, to) {
        for (var key in from) {
            to[key] = from[key];
        }
    },
    uploadFormatError() {
        n9.error("文件格式有误")
    },
    uploadTooLarge() {
        n9.error('文件大小不能超过2M')
    },
    check: {
        required(val) {
            return typeof val !== 'undefined' && val !== '';
        },
        money(val) {
            return !this.required(val) || val.isMoney();
        }
    }
}
export default n9;